package com.koron.sinoprobe.web.ledger.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.OrderItem;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.web.ledger.bean.entity.SpDocument;
import com.koron.sinoprobe.web.ledger.bean.query.SpDocumentQuery;
import com.koron.sinoprobe.web.ledger.bean.vo.SpDocumentVO;
import com.koron.sinoprobe.web.ledger.service.SpDocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author twcao
 * @title: SpDocumentController
 * @projectName GDH_EAM_V2
 * @description: 专项台账-项目文档
 * @date 2021/12/149:21
 */
@Slf4j
@RestController
@RequestMapping("/sp/project/document/")
@Api(tags = "专项台账-项目文档")
public class SpDocumentController {

    @Autowired
    private SpDocumentService documentService;

    @GetMapping("/selectOne")
    @ApiOperation("项目文档详情查询")
    @ApiImplicitParam(name = "id", value = "项目文档id", required = true)
    public Response<SpDocument> selectOne(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        SpDocument detail = ADOConnection.runTask(user.getCurrDs(), documentService, "queryById", SpDocument.class, id);
        return Response.ok("查询成功", detail);
    }

    @GetMapping("/queryList")
    @ApiOperation("项目文档分页查询， 未分页")
    public Response<PageInfo<SpDocumentVO>> queryList(@CasUser @ApiIgnore EamUser user, SpDocumentQuery query) {

        List<OrderItem> orders = query.getOrders();
        if (!CollectionUtils.isEmpty(orders)){
            OrderByInterceptor.setOrderBy(orders);
            PageHelper.startPage(query.getPage(), query.getPageSize());
        } else{
            query.setPage((query.getPage()-1) * query.getPageSize());
        }
        List<SpDocumentVO> list = ADOConnection.runTask(user.getCurrDs(), documentService, "queryList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改项目文档")
    public Response<SpDocument> saveOrUpdate(@CasUser @ApiIgnore EamUser user, @RequestBody SpDocument detail) {
        detail = ADOConnection.runTask(user.getCurrDs(), documentService, "saveOrUpdate", SpDocument.class, detail);
        return Response.ok("操作成功", detail);
    }

    @GetMapping("/deleteById")
    @ApiOperation("根据id删除项目文档")
    @ApiImplicitParam(name = "id", value = "项目文档id", required = true)
    public Response<String> deleteById(@CasUser @ApiIgnore EamUser user, @RequestParam String id) {
        ADOConnection.runTask(user.getCurrDs(), documentService, "deleteById", Boolean.class, id);
        return Response.ok("删除成功", id);
    }
    
}
